#include <stdio.h>
#include <stdlib.h>

// 二叉树代码实现01 https://www.bilibili.com/video/BV1Qx411m7Lb
// 二叉树的定义：根比左边的大，比右边的小
// 根>左
// 根<右
typedef struct node{
    int data;
    struct node* left;
    struct node* right;
} Node;

// 先序遍历(根左右)
void preorder(Node *node){
    if (node != NULL)
    {
       printf("%d ", node->data);
       preorder(node->left);
       preorder(node->right);
    }
    
}

// 中序遍历（左根右）
void inorder(Node *node){
    if (node != NULL)
    {
       inorder(node->left);
       printf("%d ", node->data);
       inorder(node->right);
    }
    
}

// 后序遍历（左右根）
void afterorder(Node *node){
    if (node != NULL)
    {
       afterorder(node->left);
       afterorder(node->right);
       printf("%d ", node->data);
    }
    
}

int main(){
    Node n1;
    Node n2;
    Node n3;
    Node n4;

    n1.data = 5;
    n2.data = 6;
    n3.data = 7;
    n4.data = 8;

    n1.left = &n2;
    n1.right = &n3;

    n2.left = &n4;
    n2.right = NULL;

    n3.left = NULL;
    n3.right = NULL;

    n4.left = NULL;
    n4.right = NULL;

    preorder(&n1);
    printf("%s", "\n");

    inorder(&n1);
    printf("%s", "\n");

    afterorder(&n1);
}